package final2107.day11;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Demo02 {
    public static void main(String[] args) throws IOException {
        //1.读取文件中的数据并显示
//        BufferedReader br = new BufferedReader(
//                new InputStreamReader(
//                        new FileInputStream("a.txt")));
//        //按行读取并显示
//        String line = null;
//        while ((line=br.readLine())!=null){
//            System.out.println(line);
//        }
//
//        br.close();

        //2.输出所有性别为男的数据
//        BufferedReader br = new BufferedReader(
//                new InputStreamReader(
//                        new FileInputStream("a.txt")));
//        //略过表头
//        String title = br.readLine();
//        System.out.println(title);
//        String line = null;
//        while ((line=br.readLine())!=null){
//            //判断性别是否为男
//            String sex = line.split(",")[2].trim();
//            if ("男".equals(sex)){
//                System.out.println(line);
//            }
//        }
//        br.close();

        //3.根据年龄降序排列输出
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream("a.txt")));
        //略过表头
        String title = br.readLine();
        List<String> list = new ArrayList<>();

        String line = null;
        while ((line=br.readLine())!=null){
            list.add(line);
        }
        //对list自定义排序
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.split(",")[3].trim()
                        .compareTo(o1.split(",")[3].trim());
            }
        });

        //4.将结果写入文件
        PrintWriter pw = new PrintWriter(
                new FileOutputStream("result1.txt"),true);

        System.out.println(title);
        pw.println(title);
        list.forEach(e-> {
            System.out.println(e);
            pw.println(e);
        });

        br.close();
    }
}
